﻿using Microsoft.Extensions.Configuration;
using static System.Console;

// 参考：
// https://mp.weixin.qq.com/s/q5XZsIxwHQPGbVAtROtEgQ

// 全局依赖包
// dotnet add package Microsoft.Extensions.Configuration
// dotnet add package Microsoft.Extensions.Configuration.Abstractions


// 一. 内存配置
static void MemoryConfig()
{
    // 1. 构建器
    var builder = new ConfigurationBuilder();

    // 2. 加载配置资源(内存)
    var dic = new Dictionary<string, string>
    {
        { "Server:Host", "0.0.0.0" },
        { "Server:Port", "8001" }
    };
    builder.AddInMemoryCollection(dic!);

    // 3. 解析配置项
    var config = builder.Build();
    var section = config.GetSection("Server");
    WriteLine($"内存配置: {{ Host:{section["Host"]}, Port:{section["Port"]} }}");
}

MemoryConfig();


// 二. 环境变量
// 添加: dotnet add package Microsoft.Extensions.Configuration.EnvironmentVariables
// 添加环境变量(环境变量不是别冒号，须替换为双下划线):
// export Server__Host=127.0.0.1
// export Server__Name=8002
static void EnvConfig()
{
    // 1. 构建器
    var builder = new ConfigurationBuilder();

    // 2. 加载配置资源(环境变量)
    builder.AddEnvironmentVariables();

    // System.Environment.SetEnvironmentVariable("Server:Host", "localhost");

    // 3. 解析配置项
    var config = builder.Build();
    var host = config.GetSection("Server:Host").Value;
    var port = config.GetSection("Server:Port").Value;
    WriteLine($"环境变量: {{ Host:{host}, Port:{port} }}");
}

EnvConfig();


// 三. 命令行变量
// 添加: dotnet add package Microsoft.Extensions.Configuration.CommandLine
static void CommandLineConfig(string[] args)
{
    // 1. 构建器
    var builder = new ConfigurationBuilder();

    // 2. 加载配置资源(命令行)
    builder.AddCommandLine(args);

    // 3. 解析配置项
    var config = builder.Build();
    var section = config.GetSection("Server");
    WriteLine($"命令行:   {{ Host:{section["Host"]}, Port:{section["Port"]} }}");
}

CommandLineConfig(args);
// 调用：dotnet run --Server:Host=localhost --Server:Port=8003


// 四. Json配置
// 添加: dotnet add package Microsoft.Extensions.Configuration.Json
static void JsonConfig()
{
    // 1. 构建器
    var builder = new ConfigurationBuilder();

    // 2. 加载配置资源(Json文件)
    builder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

    // 3. 解析配置项
    var config = builder.Build();
    var section = config.GetSection("Server");
    WriteLine($"Json文件: {{ Host:{section["Host"]}, Port:{section["Port"]} }}");
}

JsonConfig();


// 6. XML配置
// 添加: dotnet add package Microsoft.Extensions.Configuration.Xml
static void XmlConfig()
{
    // 1. 构建器
    var builder = new ConfigurationBuilder();

    // 2. 加载配置资源(Xml文件)
    builder.AddXmlFile("appsettings.xml");

    // 3. 解析配置项
    var config = builder.Build();
    var section = config.GetSection("Server");
    WriteLine($"Xml文件:  {{ Host:{section["Host"]}, Port:{section["Port"]} }}");
}

XmlConfig();

// 七. 配置绑定对象
// 添加: dotnet add package Microsoft.Extensions.Configuration.Binder
static void BindObject()
{
    // 1. 构建器
    var builder = new ConfigurationBuilder();

    // 2. 加载配置资源
    builder.AddXmlFile("appsettings.xml");

    // 3. 解析配置项
    var config = builder.Build();
    var section = config.GetSection("Server");

    // 4. 绑定对象
    var server = new Server();
    section.Bind(server);
    WriteLine($"绑定对象1: {{ Host:{server.Host}, Port:{server.Port} }}");

    // 或
     server = section.Get<Server>();
    WriteLine($"绑定对象2: {{ Host:{server.Host}, Port:{server.Port} }}");
}

BindObject();

public record Server
{
    public string Host { get; set; }
    public int Port { get; set; }
}